/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fei.ClassDiagram.WindowComponents;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import org.fei.ClassDiagram.drawing.Lines.AsotiationArrow;
import org.fei.ClassDiagram.drawing.Lines.Line;
import org.fei.ClassDiagram.drawing.Classes.Drawing;

/**
 *
 * @author Tomas
 */
public class Diagram extends JPanel implements MouseListener, MouseMotionListener, ItemListener {
    private ArrayList<Drawing> classes;
    private ArrayList<Line> lines;
    private Drawing actual;
    private int tmpX, tmpY;
    private boolean isAsociationEnabled = true;
    
    public Diagram(ArrayList<Drawing> classes, ArrayList<Line> lines) {
        this.classes = classes;
        this.lines = lines;
        this.actual = null;
        
        this.setBackground(Color.white);

        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        for (Drawing d : classes) {
            d.draw(g);
        } 
        
        for (Line l : lines) {
            if (l instanceof AsotiationArrow) {
                if (this.isAsociationEnabled)
                    l.draw(g);
            }
            else {
                l.draw(g);
            }
        }
        
        g.dispose();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        for (Drawing d: classes) {
            if (d.isOver(e.getX(), e.getY())) {
                actual = d;
                tmpX = (e.getX()-actual.getCornerX());
                tmpY = (e.getY()-actual.getCornerY());
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        actual = null;
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (actual != null) {
            actual.setCornerX(e.getX() - tmpX);
            actual.setCornerY(e.getY() - tmpY);
            this.repaint();
        }
        
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        //Message.showMessage(String.valueOf(((JCheckBox)e.getItemSelectable()).isSelected()));
        this.isAsociationEnabled = ((JCheckBox)e.getItemSelectable()).isSelected();
        if (!isAsociationEnabled) {
            for (Drawing d : classes) {
                d.setIsAsotiationClass(isAsociationEnabled);
            }
        }
        else {
            for (Drawing d: classes) {
                for (Line l : lines) {
                    if (l instanceof AsotiationArrow) {
                        if (d.getRightPoint() == l.getEnd()) {
                            d.setIsAsotiationClass(isAsociationEnabled);
                        }
                    }
                }
            }    
        }
        //Message.showMessage(String.valueOf(this.isAsociationEnabled));
        this.repaint();
    }
    
    public void setDiagramSize(int width, int height) {
        this.setPreferredSize(new Dimension(width, height));
    }
}
